﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using KingRoad.EPM.SafetyManagementBLL.UIRules;
using KingRoad.EPM.SafetyManagementEntity.Model;
using KingRoad.EPM.Web.Areas.SafetyConstruction.ViewModels;
using KingRoad.EPM.Web.Areas.SafetyConstruction.SafetyConstructionFramework.Common;

namespace KingRoad.EPM.Web.Areas.SafetyConstruction.Controllers
{
    public class ScmSecureCostController : BaseController
    {
        //
        // GET: /ScmSecureCost/

        /// <summary>
        /// 安全费用信息管理
        /// </summary>
        /// <returns></returns>
        public ActionResult SecureCostIndex()
        {
            var model = new ScmSecureCostinfoViewModel();
            model.TheUnitList = GetUnitListByType(1); ;
            return View(model);
        }
        /// <summary>
        /// 安全费用列表
        /// </summary>
        /// <returns></returns>
        public ActionResult SecureCostList(string Section, string Type)
        {
            ViewBag.Section = Section;
            string sql = "";
            if (Section != null)
            {
                sql = "Sc_Section='" + Section + "'";
            }
            if (Type != null)
            {
                sql = "Sc_SectionType='" + Type + "'";
            }
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = 1;
            //每页多少条
            ViewBag.numPerPage = 20;
            var securecostList = ScmSecurecostUIRule.GetListForPage(ViewBag.pageNum, ViewBag.numPerPage, "Sc_SectionType", out totalRecord, sql);
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View(new ScmSecureCostinfoViewModel { TheSecureCostinfoList = securecostList });
        }
        /// <summary>
        /// 安全费用-列表(分页提交)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SplitPage()
        {
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = Convert.ToInt32(Request.Form["pageNum"]);
            //每页多少条
            ViewBag.numPerPage = Convert.ToInt32(Request.Form["numPerPage"]);
            ViewBag.Section = Request.Form["input_section"];
            string sql = " 1=1 ";

            if (ViewBag.Section != "")
                sql += " and Sc_Section= '" + ViewBag.Section + "' ";
            var consPersonnelList = ScmSecurecostUIRule.GetListForPage(ViewBag.pageNum, ViewBag.numPerPage, "Sc_SectionType", out totalRecord, sql);
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View("SecureCostList", new ScmSecureCostinfoViewModel { TheSecureCostinfoList = consPersonnelList });
        }
        /// <summary>
        /// 安全费用添加
        /// </summary>
        /// <returns></returns>
        public ActionResult SecureCostAdd(string Section)
        {
            var model = new ScmSecureCostinfoViewModel();
            model.TheSecureCostinfo = new ScmSecurecostInfo();
            model.TheSecureCostinfo.Sc_ID = Guid.NewGuid().ToString();
            model.TheSecureCostinfo.Sc_Section = Section;
            ProjectUnitInfo uInfo = GetUnitInfoByCode(Section);


            return View(model);
        }
        /// <summary>
        ///安全费用信息-添加 
        /// 功能：执行添加操作
        /// </summary>
        /// <returns></returns>
        public JsonResult SecureCostAddData(ScmSecureCostinfoViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                //TODO:操作人信息
                vm.TheSecureCostinfo.Sc_UserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheSecureCostinfo.Sc_UserName = GetLoginUserRealName;
                vm.TheSecureCostinfo.Sc_Time = DateTime.Now;
                var safe = ScmSecurecostUIRule.InsertEntity(vm.TheSecureCostinfo);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "添加成功",
                    navTabId = "xtgl_aqfygl",
                    rel = "",
                    callbackType = "closeCurrent",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "添加失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        /// <summary>
        /// 安全费用信息-删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult SecureCostDelete(string id)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                var constru = ScmSecurecostUIRule.DeleteEntity(id);
            });
            dynamic jsonReturn = new ExpandoObject();
            //删除成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "删除成功",
                    navTabId = "xtgl_aqfygl",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "删除失败",
                };
            }
            return new JsonResult()
            {
                Data = jsonReturn
            };

        }

        /// <summary>
        /// 安全费用信息——编辑
        /// 功能：根据id加载相对应数据
        /// </summary>
        /// <returns></returns>
        public ActionResult SecureCostEdit(Guid id)
        {
            var securecostInfo = ScmSecurecostUIRule.GetInfoByID(id);
            return View(new ScmSecureCostinfoViewModel { TheSecureCostinfo = securecostInfo });
        }
        /// <summary>
        /// 安全费用信息-编辑（保存工作） 
        /// 功能：执行保存数据
        /// </summary>
        /// <returns></returns>
        public JsonResult SecureCostEditData(ScmSecureCostinfoViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                //TODO:操作人信息
                vm.TheSecureCostinfo.Sc_UserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheSecureCostinfo.Sc_UserName = GetLoginUserRealName;
                vm.TheSecureCostinfo.Sc_Time = DateTime.Now;
                var construction = ScmSecurecostUIRule.UpdateEntity(vm.TheSecureCostinfo);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "更新成功",
                    navTabId = "xtgl_aqfygl",
                    rel = "",
                    callbackType = "closeCurrent",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "更新失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }


        /// <summary>
        /// 安全费用信息——查看
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public ActionResult SecureCostDetail(Guid id)
        {
            var ConstructionInfo = ScmSecurecostUIRule.GetInfoByID(id);
            return View(new ScmSecureCostinfoViewModel { TheSecureCostinfo = ConstructionInfo });
        }


    }
}
